<!--
 * Copyright © 2022 XXXXXXXXX
 * @Author: zhangxiang
 * @Date: 2022-08-04 18:01:36
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-09-21 15:08:00
 * @Description:
 *
-->

<template>
  <div class="service-mgmt-ctn">
    <el-row type="flex" style="margin-bottom: 20px">
      <el-input
        v-model="searchKey"
        style="width: 200px; margin-right: 20px"
        size="small"
        class="input"
        placeholder="请输入关键字进行过滤"
        clearable
        @clear="clearClick"
        @keyup.enter.native="queryClick"
      />
      <el-button
        size="small"
        type="primary"
        @click="queryClick"
      >查询</el-button>
      <el-button
        size="small"
        type="primary"
        style="margin-left: auto"
        icon="el-icon-plus"
        @click="createClick"
      >创建服务</el-button>
    </el-row>
    <el-table
      key="svc"
      v-loading="loading"
      class="svc-table table-fixed"
      :data="list"
      header-cell-class-name="listHeaderCell"
      :header-cell-style="{ background: '#F3F3F3', color: '#696969' }"
      border
    >
      <el-table-column
        prop="cloudVendor"
        label="云商名称"
        min-width="110"
        show-overflow-tooltip=""
      >
        <template slot-scope="scope">
          {{ cloudVendorToChn(scope.row.cloudVendor) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="k8sClusterName"
        label="集群名称"
        min-width="180"
        show-overflow-tooltip=""
      />
      <el-table-column
        prop="svcName"
        label="服务名称"
        min-width="160"
        show-overflow-tooltip=""
      />
      <el-table-column
        prop="namespace"
        label="命名空间"
        min-width="130"
        show-overflow-tooltip=""
      />
      <el-table-column
        prop="svcType"
        label="服务类型"
        width="120"
        show-overflow-tooltip=""
      />
      <el-table-column prop="clusterIp" label="Cluster IP" width="150" />
      <el-table-column
        prop="port"
        label="端口"
        min-width="170"
      />
      <el-table-column
        prop="seclector"
        label="选择器"
        min-width="140"
        show-overflow-tooltip=""
      />
      <el-table-column prop="status" label="状态" width="100" />
      <el-table-column prop="createTimeStr" label="创建时间" width="160" />
      <el-table-column label="操作" width="210" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            style="height: 22px; line-height: 22px; padding: 0 10px"
            @click="toPodsClick(scope.row)"
          >关联实例</el-button>
          <el-button
            type="primary"
            size="mini"
            style="height: 22px; line-height: 22px; padding: 0 10px"
            @click="editClick(scope.$index)"
          >编辑</el-button>
          <el-popover
            :ref="`popover-delete-${scope.$index}`"
            placement="top"
            width="300"
            style="margin-left: 10px"
          >
            <p>是否确认删除该服务？</p>
            <div style="text-align: right; margin: 0">
              <el-button
                size="mini"
                type="text"
                @click="
                  scope._self.$refs[`popover-delete-${scope.$index}`].doClose()
                "
              >取消</el-button>
              <el-button
                type="primary"
                size="mini"
                @click="deleteClick(scope)"
              >确定</el-button>
            </div>
            <el-button
              slot="reference"
              type="danger"
              style="height: 22px; line-height: 22px; padding: 0 10px"
              size="mini"
            >删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end" style="margin-top: 25px">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next"
        :page-sizes="[10, 15, 20]"
        :page-size="pageSize"
        :current-page.sync="pageNum"
        :total="totalNum"
        @current-change="changePage"
        @size-change="sizeChange"
      />
    </el-row>

    <!--创建/编辑 服务-->
    <el-dialog
      class="svc-create-dialog"
      :title="dialogType === 1 ? '创建服务' : '编辑服务'"
      :visible="showDialog"
      width="800px"
      top="13vh"
      :close-on-click-modal="false"
      @close="showDialog = false"
    >
      <div class="title-wrapper">
        <div class="title">
          {{ dialogType === 1 ? "按yaml文件创建" : "yaml文件更新" }}
        </div>
      </div>
      <yaml-editor
        v-model="dialogYaml"
        class="create-yaml-wrapper"
        :height="'50vh'"
      />
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDialog = false">取 消</el-button>
        <el-button
          type="primary"
          size="small"
          :loading="dialogConfirmLoading"
          :disabled="!dialogYaml"
          @click="dialogConfirmClick"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import YamlEditor from '@/components/YamlEditor'
import commonTab from '../mixin/commonTab'
import {
  getK8sClusterSvcs,
  addK8sClusterSvc,
  editK8sClusterSvc,
  delK8sClusterSvc
} from '@/api/cnap/hybrid-cloud'

export default {
  name: 'ServiceMgmt',
  components: {
    YamlEditor
  },
  mixins: [commonTab],
  data() {
    return {
      showDialog: false, // 展示弹窗
      dialogType: 1, // 1-创建 2-编辑
      dialogYaml: '', // yaml
      dialogConfirmLoading: false, // 弹窗确定按钮loading
      editIndex: 0 // 编辑数据的index
    }
  },
  methods: {
    /**
     * @description: 创建确定
     * @return {*}
     */
    dialogConfirmClick() {
      this.dialogConfirmLoading = true
      if (this.dialogType === 1) {
        // 创建
        // 添加
        addK8sClusterSvc({
          ...this.clusterParams,
          yaml: this.dialogYaml
        })
          .then((res) => {
            this.$message.success({
              message: '创建服务成功！',
              duration: '1500'
            })
            this.showDialog = false
            this.pageNum = 1 // 回到第一页
            this.getSvcsInfo() // 刷新数据
          })
          .finally(() => {
            this.dialogConfirmLoading = false
          })
      } else if (this.dialogType === 2) {
        // 编辑
        // 编辑数据
        editK8sClusterSvc({
          ...this.clusterParams,
          svcName: this.list[this.editIndex].svcName,
          namespace: this.list[this.editIndex].namespace,
          yaml: this.dialogYaml
        })
          .then((res) => {
            this.$message.success({
              message: '编辑服务成功！',
              duration: '1500'
            })
            this.showDialog = false
            this.getSvcsInfo() // 刷新数据
          })
          .finally(() => {
            this.dialogConfirmLoading = false
          })
      }
    },
    /**
     * @description: 编辑服务
     * @param {*} index
     * @return {*}
     */
    editClick(index) {
      this.editIndex = index // 编辑数据的index
      this.dialogYaml = this.list[index].yaml || '' // 编辑数据的yaml
      this.dialogType = 2 // 编辑弹窗
      this.showDialog = true // 展示弹窗
    },
    /**
     * @description: 创建服务
     * @return {*}
     */
    createClick() {
      this.dialogYaml = ''
      this.dialogType = 1 // 创建弹窗
      this.showDialog = true // 展示弹窗
    },
    /**
     * @description: 关联实例
     * @param {*} row
     * @return {*}
     */
    toPodsClick(row) {
      // 跳转链接
      const fullPath = `${this.$route.path}?cloudVendor=${row.cloudVendor}&accountName=${row.accountName || ''}&k8sClusterName=${row.k8sClusterName}&targetTab=pods&svcName=${row.svcName}&namespace=${row.namespace}`
      // 跳转去容器组
      this.$router.replace({
        path: '/redirect' + fullPath
      })
    },
    /**
     * @description: 删除
     * @param {*} scope
     * @return {*}
     */
    deleteClick(scope) {
      const row = scope.row
      // 请求
      delK8sClusterSvc({
        ...this.clusterParams,
        namespace: row.namespace,
        svcName: row.svcName
      }).then((res) => {
        scope._self.$refs[`popover-delete-${scope.$index}`].doClose() // 取消popover
        this.$message.success('删除成功')
        if (this.list.length === 1) {
          // 当前页只有一条数据时 删除完刷新上一页数据
          this.pageNum = Math.max(this.pageNum - 1, 1)
        }
        this.getSvcsInfo() // 刷新数据
      })
    },
    /**
     * @description: 每页显示size变化
     * @param {*} size
     * @return {*}
     */
    sizeChange(size) {
      this.pageSize = size
      this.pageNum = 1
      this.getSvcsInfo() // 请求数据
    },
    /**
     * @description: 当前页改变
     * @param {*} pageNum
     * @return {*}
     */
    changePage(pageNum) {
      this.pageNum = pageNum
      this.getSvcsInfo() // 请求数据
    },
    /**
     * @description: 清除关键字
     * @return {*}
     */
    clearClick() {
      this.searchKey = ''
      this.pageNum = 1
      this.getSvcsInfo() // 请求数据
    },
    /**
     * @description: 查詢
     * @return {*}
     */
    queryClick() {
      this.pageNum = 1
      this.getSvcsInfo() // 请求数据
    },
    /**
     * @description: 获取服务
     * @return {*}
     */
    getSvcsInfo() {
      // 请求
      getK8sClusterSvcs({
        ...this.clusterParams,
        keyword: this.searchKey,
        pageNo: this.pageNum,
        pageSize: this.pageSize
      })
        .then((res) => {
          if (res.data) {
            this.totalNum = res.data.totalNum
            this.list = Array.isArray(res.data.result) ? res.data.result : []
          } else {
            // 数据为空处理
            this.totalNum = 0
            this.list = []
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    /**
     * @description: 初始化
     * @return {*}
     */
    initTabData() {
      this.loading = true
      this.pageNum = 1
      this.getSvcsInfo()
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.service-mgmt-ctn {
  .svc-table {
    /deep/ .el-table::before {
      background-color: #e9e9e9;
    }
    /deep/ .listHeaderCell {
      height: 40px;
      padding: 0;
      box-sizing: border-box;
      &:first-child {
        padding-left: 20px;
      }
    }
    /deep/ .el-table__body {
      .el-table__row td {
        height: 40px;
        padding: 0;
        &:first-child {
          padding-left: 20px;
        }
      }
    }
  }
  .svc-create-dialog {
    .title-wrapper {
      border-bottom: 1px solid #f2f2f2;
      margin-bottom: 12px;
      .title {
        box-sizing: border-box;
        display: inline-block;
        .textStyle(14px , 500, #0091ff);
        border-bottom: 2px solid #0091ff;
        padding: 4px 0;
      }
    }
  }
}
.svc-edit-dialog {
  .formItem {
    display: inline-block;
    width: calc(50% - 20px);
    &.left {
      margin-right: 40px;
    }
  }
}
</style>
